﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Net.Mail;
using System.Net;

namespace WebSendMail
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string upl = Server.MapPath("~/Upload/");
            if (!Directory.Exists(upl))
            {
                Att.Visible = false;
            }
            else
            {
                Att.Visible = true;
                Att.Items.Clear();
                List<string> files = Directory.GetFiles(upl).ToList<string>();
                foreach (string file in files)
                {
                    FileInfo path = new FileInfo(file);
                    Att.Items.Add(path.Name);
                }
            }
        }

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUploadAttachment.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUploadAttachment.FileName);
                    string upl = Server.MapPath("~/Upload/");
                    if (!Directory.Exists(upl))
                        Directory.CreateDirectory(upl);
                    FileUploadAttachment.SaveAs(upl + filename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                     
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;

                }
            }
            
        }

        protected void Carica_Click(object sender, EventArgs e)
        {
            string filename = Path.GetFileName(FileUploadTo.FileName);
            string upl = Server.MapPath("~/Send/");
            if (!Directory.Exists(upl))
                Directory.CreateDirectory(upl);
            FileUploadTo.SaveAs(upl + filename);
            List<string> list = new List<string>();
            using (StreamReader reader = new StreamReader(upl + filename))
            {
                while (!reader.EndOfStream)
                {
                    string read = reader.ReadLine();
                    list.Add(read);
                }
            }

            foreach (string read in list)
            {
                TextTo.Text += read+"\n";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage();
            
            string email = tMail.Text;
            string password = tPassword.Text;
            string smtpAgent = tSmtp.Text;
            string obj = TextObj.Text;
            string body = TextMail.Text;


            SmtpClient smtp = new SmtpClient(smtpAgent);
            NetworkCredential net = new NetworkCredential(email, password);
            smtp.Credentials = net;
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            string To = TextTo.Text;
            smtp.Port = 587;
            List<string> toList = new List<string>();
            char[] split = new char[] { '\n' };
            toList= To.Split(split,StringSplitOptions.RemoveEmptyEntries).ToList<string>();
            foreach (string toitem in toList)
            {
                mail.Bcc.Add(new MailAddress(toitem));
            }

            mail.To.Add(new MailAddress(email));
            mail.From = new MailAddress(email);

            try
            {
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
            }
        }
    }
}